<!--  -->
<template>
  <div class="create-poster">
    绘
    <div
      class="mask"
      @touchmove.prevent
      @mousewheel.prevent
      @click="closePosterFun"
    ></div>

    <!-- 加载的图标 -->
    <img
      src="@/assets/images/loading.gif"
      alt=""
      v-if="showloading"
      class="loadingImg"
    />

    <img
      v-else
      :src="posterImg"
      alt=""
      class="posterImg"
      @touchmove.prevent
      @mousewheel.prevent
    />
    <canvas-poster :drawData="drawData" @success="success"></canvas-poster>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      test: "",
      posterImg: "",
      showloading: true,
    };
  },
  //监听属性 类似于data概念
  computed: {
    drawData() {
      var price = Number(this.detailsData.shopPrice).toFixed(2);
      return {
        width: 360,
        height: 667,
        backgroundColor: "#fff",
        views: [
          // 绘制本地图片
          {
            type: "image",
            url: require("@/assets/images/share-1.jpg"), // 远程图片可以直接改为图片地址
            top: 20,
            left: 20,
            width: 300,
            height: 360,
          },
          // 绘制文字
          {
            type: "text",
            content: price,
            fontSize: 20,
            top: 420,
            left: 40,
            color: "red",
          },
          // 绘制文字
          {
            type: "text",
            content: this.detailsData.title,
            fontSize: 14,
            top: 450,
            left: 40,
            width: 280,
            lineNum: 2,
            lineHeight: 20,
          },
          // 绘制二维码
          {
            type: "qcode",
            text: "vue商城手机",
            top: 500,
            left: 135,
            width: 90,
            height: 90,
          },
        ],
      };
    },
  },
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    success(src) {
      this.posterImg = src;
    },
    closePosterFun() {
      this.$emit("closePoster", false);
    },
  },
  props: ["detailsData"],
  mounted() {
    setTimeout(() => {
      this.showloading = false;
    }, 1000);
  },
};
</script>
<style lang="less">
.mask {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 100;
}
.posterImg {
  width: 80%;
  height: 80%;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  background-color: #fff;
  z-index: 101;
}
.loadingImg {
  width: 10%;
  height: 10%;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  z-index: 101;
}
</style>